package com.sys.util;

import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;

@SuppressWarnings("unused")
public class JsonFileUtils {

    public static JSONObject FileGetJSON(String path) {
        path = path.replace("\\", "/");
        if (path.contains(":")) {
            path = path.replace("file:/", "");
        }
        try {
            String input = FileUtils.readFileToString(new File(path), "UTF-8");
            JSONObject jsonObject = JSONObject.fromObject(input);
            if (jsonObject != null) {
                return jsonObject;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int FileWriteJSON(String path, JSONObject value) {
        int returnCode = 0;
        path = path.replace("\\", "/");
        if (path.contains(":")) {
            path = path.replace("file:/", "");
        }
        System.out.println("开始写入...");
        try {
            String cs = value.toString();
            FileUtils.writeStringToFile(new File(path), cs, "UTF-8", false);
            returnCode = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnCode;
    }
}
